package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {


    @Autowired(required = false)
    private ItemMapper itemMapper;


    /**
     * 商品管理分页查询
     * @param page
     * @param size
     * @return
     */
   @Override
    public PageDTO<Item> Page(Integer page, Integer size) {
        Page<Item> Page1 = new Page<>(page, size);

        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();

        PageDTO<Item> pageDTO = new PageDTO<>();

       Page<Item> itemPage = itemMapper.selectPage(Page1, queryWrapper);

       pageDTO.setList(Page1.getRecords());
       pageDTO.setTotal(Page1.getTotal());



        System.out.println("总条数：" + Page1.getTotal());
        System.out.println("当前页：" + Page1.getCurrent());
        System.out.println("总页数：" + Page1.getPages());
        System.out.println("每页大小：" + Page1.getSize());
        System.out.println("当前页数据列表：");

        return  pageDTO;

    }

    /**
     * 商品id查询商品
     * @param id
     * @return
     */
    @Override
    public Item findById(Long id) {
      return itemMapper.selectById(id);
    }

    /**
     * 添加商品
     * @param item
     */
    @Override
    public void add(Item item) {
        item.setUpdateTime(new Date());
        item.setCreateTime(new Date());
       itemMapper.insert(item);
    }

    /**
     * 下架商品
     * @param id
     */
    @Override
    public void status(Long id) {
        itemMapper.down(id,new Date());

        }

    /**
     * 上架商品
     * @param id
     */
    @Override
    public void status1(Long id) {
        itemMapper.up(id,new Date());
    }

    /**
     * 删除商品
     * @param id
     */
    @Override
    public void delete(Long id) {
        itemMapper.deleteById(id);
    }


}